package com.ehotting.eaf.core.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class FileContentHelper {

	private static final Logger log = LoggerFactory
			.getLogger(FileContentHelper.class);

	/****
	 * 读取文件内容，
	 * 
	 * @param file
	 *            文件
	 * @param callback
	 *            内容操作对象
	 * @return
	 * @throws Exception
	 */
	public static void readContent(String file, OperateCallback callback)
			throws Exception {
		log.info("start analyze file:" + file);
		BufferedReader reader = null;
		String encoding = getFileEncode(file);
		log.info("encoding:" + encoding);

		List<String> lineList = new ArrayList<String>();
		try {
			reader = new BufferedReader(new UnicodeReader(new FileInputStream(new File(file)), encoding));
			String line = null;
			int i = 1;
			for (; (line = reader.readLine()) != null;) {
				if (StringUtils.isEmpty(line)) {
					continue;
				}
				lineList.add(line);
				i++;
				if (i % 1000 == 0) {
					callback.invoke(lineList);
					lineList.clear();
				}
			}
			callback.invoke(lineList);
			lineList.clear();
			lineList = null;
		} catch (Exception e) {
			log.error("analyze file，quit action:" + file, e);
			throw e;
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (Exception e) {
				log.info(e.getMessage());
			}
		}
		log.info("finish analyze:" + file);
	}
	
	
	/****
	 * 读取文件内容，
	 * 
	 * @param file
	 *            内容操作对象
	 * @return
	 * @throws Exception
	 */
	public static String readContent(String file)throws Exception {
		log.info("start analyze file:" + file);
		BufferedReader reader = null;
		String encoding = getFileEncode(file);
		log.info("encoding:" + encoding);
		StringBuffer buf = new StringBuffer();
		
		try {
			reader = new BufferedReader(new UnicodeReader(new FileInputStream(new File(file)), encoding));
			String line = null;
			for (; (line = reader.readLine()) != null;) {
				if (StringUtils.isEmpty(line)) {
					continue;
				}
				buf.append(line);
			}
			return buf.toString();
		} catch (Exception e) {
			log.error("analyze file，quit action:" + file, e);
			throw e;
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (Exception e) {
				log.info(e.getMessage());
			}
		}

	}

	/***
	 * 获取文件编码
	 * 
	 * @param path
	 * @return
	 */
	public static String getFileEncode(String path) {
//		File file = new File(path);
//		InputStream in = null;
//		try {
//			in = new java.io.FileInputStream(file);
//			byte[] b = new byte[3];
//			in.read(b);
//			if (b[0] == -17 && b[1] == -69 && b[2] == -65 ||(b[0] == 123 && b[1] == 34 && b[2] == 97))
//				return "UTF-8";
//			else
//				return "GBK";
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (in != null) {
//				try {
//					in.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
		return "utf-8";
	}

	/***
	 * 文件是否存在
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean isExist(String fileName) {
		return (new File(fileName)).exists();
	}

	public interface OperateCallback {
		public void invoke(List<String> lineList);
	}

	public static void main(String[] args) {


		String d = FileContentHelper.getFileEncode("C:\\Users\\think\\Desktop\\log\\ff808181731ed6f5017327062e33009c_ff80818171b950730171c4ed156b0523_ff80818172979d9901729c832c5a01e7");
		System.out.println(d);

//		OperateCallback callback = new OperateCallback() {
//
//			@Override
//			public void invoke(List<String> lineList) {
//				System.out.println("__________________________");
//				for (String s : lineList) {
//					System.out.println(s.length());
//				}
//
//			}
//		};
//
//		try {
//			FileContentHelper
//					.readContent(
//							"D:\\temp\\VTMA\\20141130\\Reconciliation\\reconciliation_B612USR8_61208_123.txt",
//							callback);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		try {
//			FileContentHelper
//					.readContent(
//							"F:\\workspace\\TestProject\\testResource\\Reconciliation\\20141130\\reconciliation_B612USR8_61208.txt",
//							callback);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

}

